<?php

/**
 * Description of specialDateCreateAction
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class specialDateCreateAction extends sfAction {

    public function execute($request) {
        $this->form = new SpecialDateForm();

        $params = $request->getParameter($this->form->getName());
        $params['date'] = Util::convertDateClientServer($params['date'], 'nl');

        if ($this->processSpecialDay($params, $this->form)) {
            if (null == $this->form->getValue('all_restaurants')) {
                SqlEngineCore::get()->insert(
                        array('table' => 'settings_special_date',
                              'columns' => array('date' => $this->form->getValue('date'),
                                                 'restaurant_id' => $this->getUser()->getAttribute('current_restaurant_id'),
                                                 'description' => $this->form->getValue('description'))));
            }
            else {
                $restaurants = SqlEngine::getRestaurants();
                foreach ($restaurants as $restaurant) {
                    SqlEngineCore::get()->insert(
                        array('table' => 'settings_special_date',
                              'columns' => array('date' => $this->form->getValue('date'),
                                                 'restaurant_id' => $restaurant['restaurant_id'],
                                                 'description' => $this->form->getValue('description'))));
                }
            }

            $this->error = 0;
        }
        else {
            $this->error = 1;
        }
    }

    protected function processSpecialDay($params, sfForm $form) {
        $form->bind($params);

        return $form->isValid();
    }
}
